import wave
import os

def is_wav_file_broken(filepath):
    try:
        with wave.open(filepath, 'rb') as file:
            file.getnframes() # Just to check if it can read the file
        return False
    except:
        return True

def check_and_remove_broken_wavs(root_folder):
    for subdir, _, files in os.walk(root_folder):
        for file in files:
            if file.endswith('.wav'):
                filepath = os.path.join(subdir, file)
                if is_wav_file_broken(filepath):
                    print(f"Broken WAV file detected: {filepath}")
                    os.remove(filepath)
                    print(f"File {filepath} has been removed.")

# Replace this with the path to the folder you want to check
folder_to_check = './generated_wav'
check_and_remove_broken_wavs(folder_to_check)
